<?php
/*
   $mtime = microtime();
   $mtime = explode(" ",$mtime);
   $mtime = $mtime[1] + $mtime[0];
   $starttime = $mtime;
*/
?> 

<?php
require_once '../../../wp-blog-header.php';
include_once('ses.php');
set_time_limit(0);
$con=new SimpleEmailService("AKIAJEB25SWREHIRO2YA","Ota+tmICcb0aX+438v1fjmNizdCzlA/fzYr+G8cy");
$con->listVerifiedEmailAddresses();
//$con->verifyEmailAddress('kumaril@enthrallmedia.com');
//exit();

$city = ucwords($_GET['city']);
$date = date('Y-m-d');
$date = '%'.$date.'%';
//echo $city;

/*
$city_pod = new Pod('city');
$city_pod->page =1;
$city_pod->search = false;
$city_pod->findRecords('id ASC', -1);
while($city_pod->fetchRecord()){
$cities[] = ucwords($city_pod->get_field('name'));
}


foreach($cities as $city_name){
$date = date('Y-m-d');
$date = '%'.$date.'%';
$city = $city_name;
unset($city_name);
*/

$users_pod = new Pod('users');
$users_pod->page =1;
$users_pod->search = false;
$where = "t.daily_newsletter=1 && city.name='$city'";
$users_pod->findRecords('id DESC', -1,$where);
$usersFound = $users_pod->getTotalRows();
//echo $usersFound;
//exit();

unset($users_array);

$i=0;
while($users_pod->fetchRecord()){
$users_array[$i]['id'] = $users_pod->get_field('id');
$users_array[$i]['wp_user_id'] = $users_pod->get_field('wp_user.ID');
$users_array[$i]['wp_user_login'] = $users_pod->get_field('wp_user.user_login');
$users_array[$i]['email'] = $users_pod->get_field('wp_user.user_email');
$users_array[$i]['name'] = $users_pod->get_field('name');
$users_array[$i]['city'] = $users_pod->get_field('city.name');
$users_array[$i]['categories'] = $users_pod->get_field('categories.url');
$users_array[$i]['categories_name'] = $users_pod->get_field('categories.name');
$i++;
}

$countynotshare = (count($users_array) + 1);
$users_array[$countynotshare]['email'] = 'deals@ynotshare.com';
$users_array[$countynotshare]['city'] =  $city;
$users_array[$countynotshare]['categories'] = array(0=>'c-health-and-beauty-offers',1=>'c-restaurants-clubs-offers',2=>'c-shopping-offers',3=>'c-entertainment-offers',4=>'c-flowers-gifts-offers',5=>'c-services-offers',6=>'c-fashion-offers');
$users_array[$countynotshare]['categories_name'] = array(0=>'Health & Beauty',1=>'Restaurants & Clubs',2=>'Shopping',3=>'Entertainment',4=>'Flowers & Gifts',5=>'Services',6=>'Fashion');
/*
echo count($users_array);
echo "<pre>";
print_r($users_array);
exit();
*/

$deals_pod = new Pod('deal');
$deals_pod->page =1;
$deals_pod->search = false;
//$where = "city.name = '".strtolower($value['city'])."' && camp_cat.url IN(".$interests_ses.") && t.camp_end > DATE_ADD(NOW(), INTERVAL '05:30' HOUR_MINUTE) && t.camp_start LIKE '".$date."' && t.daily = 1";
$where = "city.name='$city' && t.camp_end > DATE_ADD(NOW(), INTERVAL '05:30' HOUR_MINUTE) && t.camp_start LIKE '".$date."' && t.daily = 1";
//echo $where;
$deals_pod->findRecords('RAND()', 15,$where);
$dealsFound = $deals_pod->getTotalRows();
//echo $dealsFound;
//exit();

unset($deals);
$j=0;
while($deals_pod->fetchRecord()){
  $deals[$j]['name'] = stripslashes($deals_pod->get_field('name'));
  $deals[$j]['link'] = get_bloginfo('url').'/deals/'.$deals_pod->get_field('camp_link');
  $deals[$j]['category'] = $deals_pod->get_field('camp_cat.url');
  $deals[$j]['category_name'] = $deals_pod->get_field('camp_cat.name');
  $deals[$j]['oriprice'] = $deals_pod->get_field('oriprice');
  $deals[$j]['disper'] = $deals_pod->get_field('disper');
  $deals[$j]['disprice'] = $deals_pod->get_field('disprice');
  $deal_image = $deals_pod->get_field('image.guid');
  if(empty($deal_image))
  {
    $deal_image = $deals_pod->get_field('ext_image_url');
  }
  $deals[$j]['image'] = $deal_image;
  $j++;
}
//print_r($deals);
//exit();
//echo "<pre>";
//print_r($deals);
//exit();

if(count($users_array) > 0)
{

foreach($users_array as $k_users=>$v_users){
  $utime = microtime();
  $utime = explode(" ",$utime);
  $utime = $utime[1] + $utime[0];
  $ustarttime = $utime;
  
  unset($ses_deals);
  foreach($deals as $k_deals=>$v_deals){
    
    if(!is_array($v_users['categories']))
    {
      $v_users['categories'] = array($v_users['categories']);
      $v_users['categories_name'] = array($v_users['categories_name']);
    }
    
    if(in_array($v_deals['category'],$v_users['categories']))
    {
      $ses_deals[] = $deals[$k_deals]; 
    }  
  }
  
  unset($count_deals);
  $count_deals = count($ses_deals);
  
  if($count_deals > 0)
  {
    $m = new SimpleEmailServiceMessage();
    $m->addTo($v_users['email']);
    //$m->addTo('nikkhil.narang@enthrallmedia.com');
    $m->setFrom('Buzzr.in<info@enthrallmedia.com>');
    $m->setSubject('Buzzr.in: Best '.$city.' Deals for '.date('M d, Y').'.');
    $m->setReturnPath('kumaril@enthrallmedia.com');
  
    unset($html);
    $html = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title></title>
  </head>
  <body>
       <div id="maincontent" style="height: 100%; margin: auto; text-align: center; width: 620px;">
           <div id="header" style="background-color: #FFFFFF; border-bottom: 1px solid; height: 90px; position: relative; width: 100%;">
                <div id="logo" style="float: left;background-color: #FFFFFF; height: 90px; left: 0; position: absolute; text-align: left; top: 0; width: 50%;">
                    <a href="http://buzzr.in"><img style="width:50%; margin: 30px 0 0 15px; border: 0;" src="http://buzzr.in/wordpress/wp-content/plugins/buzzr_newsletter/buzzrin-logo.png" /></a>
                </div>
                <div id="date" style="float: left;background-color: #FFFFFF; height: 90px; position: absolute;  right: 0;  top: 0; width: 50%;">
                    <div style="margin: 30px 0 0 150px; letter-spacing: 0pt; font-family: Arial,Helvetica,sans-serif; font-size: 14px; color: #C60000" align="center">'.$city.'<br />'.date('M d, Y').'</div>
                </div>
            </div>
            <div id="content" style="border: 1px solid black; padding-top: 10px; width: 100%;">
              <div id="message" style="background-color: #FFFECC; border: 1px dashed; height: 40px; margin: auto; text-align: center; width: 90%;">';
  
  if($v_users['email'] != 'deals@ynotshare.com'){
  unset($encrypt);
  $encrypt = encrypt($v_users['wp_user_login'].'~'.$v_users['city'].'~'.$v_users['wp_user_id'],'1b2u3z4z5r6');
  unset($link);
  $link = 'http://buzzr.in?editpref='.$encrypt;
  }
  else{
    $link = 'http://buzzr.in/dailydeals/?utm_source=Daily+Deals&utm_campaign='.$city.'_Daily&utm_medium=ynotshare';
  }
  
  if(count($v_users['categories_name']) > 2){
    $cat = implode(', ',array_slice($v_users['categories_name'],0,2)).'.....'; 
  }
  else{
    $cat = implode(', ',$v_users['categories_name']);
  }
  
    
    $html .= '<p style="font: bold 13px Arial,Helvetica,sans-serif; margin-top: 10px;">Now you can personalise this email. <a href="'.$link.'">Click Here</a> to select your desired categories</p>
    </div>
      <div id="text" style=" background-color: #FFFFFF; height: 40px; margin-top: 20px; position: relative; width: 100%;">
                  <p style="color: #535657; font: bold 14px Arial,Helvetica,sans-serif; margin: 0 0 0 30px; text-align: left;float: left;">New deals for you in '.$cat.'</p>
         <div id="edit" style=" position: absolute; right: 30px; top: 0;"><a style="background-color: #FFFECC; border: 1px solid; color: #535657; font: 14px Arial,Helvetica,sans-serif; padding: 5px; text-decoration: none; float: none;" href="'.$link.'">Edit Preferences</a></div>
      </div>';
                
    
    foreach($ses_deals as $deal_value){
      if(!empty($deal_value['image'])){
      
      $deal_url = $deal_value['link'].'?utm_source=Daily+Deals&utm_campaign='.$city.'_Daily&utm_medium=email';
      $html .= '<div class="dynamic" style="background-color: #FFFFFF; position: relative; width: 100%;">
                  <div class="photo" style="background-color: #FFFFFF; float: left; left: 0; top: 0; width: 300px;  margin-bottom: 20px;">
                      <a href="'.$deal_url.'"><img style="border: 1px solid #818587;" src="'.$deal_value['image'].'" width="250px" height="200px" /></a>   
                      <p style="background-color: #D44040; width: 250px; margin-left: 24px; margin-top: 2px; padding: 2px 0 2px 0; font: 15px Arial,Helvetica,sans-serif; border: 1px solid #818587; color: #ffffff;">'.$deal_value['category_name'].'</p>   
                  </div>
                  <div class="title" style="float: left; padding-right: 10px; right: 0;  text-align: left; width: 300px;">
                    <a style="color: #0A7A7E; line-height: 19px; float: left; font: bold 16px Arial,Helvetica,sans-serif; text-decoration: none;" target="_blank" title="'.$deal_value['name'].'" href="'.$deal_url.'">'.$deal_value['name'].'</a>
                    <div class="prices" style="float: left; height: 140px; text-align: left; width: 310px;">';
                    
                    if(!empty($deal_value['disprice'])){ $html .= '<p style="border-bottom: 1px solid; border-bottom-color: #CBCBD5; font: 15px Arial,Helvetica,sans-serif; color: #CC0000; width: 70%; padding-bottom: 5px;"><b>You Pay</b><span class="cost2" style="padding-left: 75px;"><b>Rs.'.$deal_value['disprice'].'</b></span></p>'; }
                    if(!empty($deal_value['oriprice'])){ $html .= '<p style="border-bottom: 1px solid; border-bottom-color: #CBCBD5;font: 15px Arial,Helvetica,sans-serif; width: 70%; padding-bottom: 5px;">Original Price<span class="cost" style="padding-left: 40px;">Rs.'.$deal_value['oriprice'].'</span></p>'; }
                    if(!empty($deal_value['disper'])){ $html .= '<p style="border-bottom: 1px solid; border-bottom-color: #CBCBD5;font: 15px Arial,Helvetica,sans-serif; width: 70%; padding-bottom: 5px;">Discount<span class="cost1" style="padding-left: 70px;">'.$deal_value['disper'].'%</span></p>'; }
                    
      $html .='</div></div></div><div style="clear:both;"></div>';            
      }
    }
    
    $html .= '</div></div></body></html>';  
    
    //print_r($html);
    //exit();
    
    $m->setMessageFromString('',$html);
    $con->sendEmail($m);
   // echo $k_users.' Sent<br />';
    //sleep(2);
   /*
   $utime = microtime();
   $utime = explode(" ",$utime);
   $utime = $utime[1] + $utime[0];
   $uendtime = $utime;
   $utotaltime = ($uendtime - $ustarttime);
   echo "This mail sent in ".$utotaltime." seconds";
   */
  }
}

}
//echo $city.' Sent<br /><br />';
//sleep(60);
//}
?>

<?php
/*
   $mtime = microtime();
   $mtime = explode(" ",$mtime);
   $mtime = $mtime[1] + $mtime[0];
   $endtime = $mtime;
   $totaltime = ($endtime - $starttime);
   echo "This page was created in ".$totaltime." seconds";
*/
?>